<template>
  <el-dialog v-model="visible" :title="!dataForm.imageId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="图片名" prop="filename">
        <el-input v-model="dataForm.filename" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="URL" prop="url">
        <!-- <el-input v-model="dataForm.url" placeholder="路径"></el-input> -->
        <small-image :style="`--my-width: ${100}px; --my-height: ${100}px;`" :url="dataForm.url" />
        
      </el-form-item>
      <el-form-item label="大小" prop="fileSize">
        <el-input v-model="dataForm.fileSize" placeholder="大小"></el-input>
      </el-form-item>
      <el-form-item label="推荐状态" prop="isRecommend">
        <el-radio-group v-model="dataForm.isRecommend">
          <el-radio :label="1">推荐</el-radio>
          <el-radio :label="0">不推荐</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="归属类型" prop="imageBelong">
        <ren-select v-model="dataForm.imageBelong" dict-type="image_belong" placeholder="归属类型"></ren-select>
      </el-form-item>
      <el-form-item label="归属id" prop="belongId">
        <el-input v-model="dataForm.belongId" placeholder="归属id"></el-input>
      </el-form-item>
      <el-form-item label="图片类型" prop="imageType">
        <ren-select v-model="dataForm.imageType" dict-type="image_type" placeholder="图片类型"></ren-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="isActive">
        <el-radio-group v-model="dataForm.isActive">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import SmallImage from "@/components/small-image/index.vue";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  imageId: "",
  filename: "",
  description: "",
  fileSize: "",
  imageType: "",
  imageBelong: "",
  belongId: "",
  updatedAt: "",
  url: "",
  isRecommend: "",
});

const rules = ref({
});

const init = (imageId?: number) => {
  visible.value = true;
  dataForm.imageId = imageId;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (imageId) {
    getInfo(imageId);
  }
};

// 获取信息
const getInfo = (imageId: number) => {
  baseService.get("/sys/images/" + imageId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.imageId ? baseService.post : baseService.put)("/sys/images", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>